﻿--[[
   @brief    
   @author   rickonzhang
   @version  1.0
   @date     2015-08-25
--]]


pop_wnd_item = {
    TALK_MSG_TYPE = 0;
    SYSTEM_MSG_TYPE = 1;
}

function pop_wnd_item:new(parent_id, msg, type)
    local obj = {parent_id_ = parent_id, msg_ = msg, type_ = type};
    setmetatable(obj, self);
    self.__index = self;
    obj:init();
    return obj;
end

function pop_wnd_item:init()
    self.root_id_ = layout_mgr.restore_sub_layout_multi(self.parent_id_, "social\\right_corner_pop_wnd_item.tpf");
    self.ctrl_ =ui_utility.get_ctrl_ptr_by_id(self.root_id_);
    self:update_ui();
end

function pop_wnd_item:delete()
    layout_mgr.remove_ctrl(self.root_id_);
    self.root_id_ = nil;
    self.ctrl_ = nil;
end

function pop_wnd_item:get_itme_type()
    return self.type_;
end

function pop_wnd_item:get_item_root_id()
    return self.root_id_;
end

function pop_wnd_item:get_size()
    local rc = self.ctrl_:GetRectEx(TRUE,TRUE);
    local w = rc.right - rc.left;
    local h = rc.bottom - rc.top;
    return w, h;
end

function pop_wnd_item:move(x, y)
    local rc = self.ctrl_:GetRectEx(TRUE,TRUE);
    local w = rc.right - rc.left;
    local h = rc.bottom - rc.top;

    rc.left = x;
    rc.right = rc.left + w;
    rc.top = y;
    rc.bottom = rc.top + h;
    self.ctrl_:MoveEx(rc,TRUE);
end

function pop_wnd_item:update_ui()
    if self.type_ == pop_wnd_item.TALK_MSG_TYPE then
        self:update_talk_msg_ui();
    else
        -- self:update_sys_msg_ui();
    end
end

function pop_wnd_item:update_talk_msg_ui()
    if self.msg_.face_url then
        self:set_data("TinyContainer_Header", "Background", string.format("ONEIMAGE:client_social\\default_head.png|%s;Circle;0;", self.msg_.face_url));
    else
        -- 这是一个群，获取群信息，提取群头像
        local group_info = group_list_model.get_group_info(self.msg_.id);
        if group_info ~= nil then
            if group_info.group_type == "MobileDiscus" then
                self:set_data("TinyContainer_Header", "Background", string.format("ONEIMAGE:client_social\\group.png|%s;Circle;0;", LoadStr("SOCIA_DISCUSS_IMG_URL")));
            else
                self:set_data("TinyContainer_Header", "Background", string.format("ONEIMAGE:client_social\\group.png|%s;Circle;0;", group_info["face_url"]));
            end
        else
            self:set_data("TinyContainer_Header", "Background", string.format("ONEIMAGE:client_social\\group.png;Circle;0;"));
        end
    end

    self:set_data("TinyStatic_NickName", "Caption", self.msg_.name);

    local format_str = social_utility.format_msg(self.msg_.msg.message);
    if self.msg_.type == 1 then --群消息需要特殊处理下
        if self.msg_.msg.msg_type == 1 then
            format_str = social_utility.format_discussion_msg(self.msg_.msg);
            if format_str == nil then
                format_str = LoadStr("SOCIAL_RIGHT_CORNER_POP_WND_GROUP_INFO_UPDATE");
            end
        end
        if format_str == nil then
            format_str = "";
        end
    end    
    self:set_data("TinyStatic_LastMsg", "Caption", format_str);

    self:set_reddot_count(self.msg_.count);
end

function pop_wnd_item:update_sys_msg_ui()
    self:set_data("TinyStatic_LastMsg", "Visible", "false");

    local ui_str, str_holder = U8toCUIStr("TinyStatic_NickName");
    local uilib = ui_utility.uilib();
    local id = uilib:GetControlIDbyPath(ui_str, self.root_id_);
    local ptr =ui_utility.get_ctrl_ptr_by_id(id);
    local rc = ptr:GetRectEx(FALSE,TRUE);
    local w = rc.bottom - rc.top;
    rc.top = rc.top + 8;
    rc.bottom = rc.top + w;
    ptr:MoveEx(rc,TRUE);

    self:set_data("TinyStatic_NickName", "Caption", LoadStr("SOCIAL_RIGHT_CORNER_POP_WND_MESSAGE_COME"));
    self:set_data("TinyContainer_Header", "Background", string.format("ONEIMAGE:social_network\\common_icon\\sys_message.png;Circle;0;"));
    local out = comm_center.call_service('i_service_msg_box_get_unread_msg_count', {});
    if nil ~= out then
        self:set_reddot_count(out.count); 
    end
end

function pop_wnd_item:set_data(ctrl_name, key, vaule)
    local uilib = ui_utility.uilib();
    local ui_str, str_holder = U8toCUIStr(ctrl_name);
    local id = uilib:GetControlIDbyPath(ui_str, self.root_id_);
    ui_utility.setproperty_by_id(id, key, vaule);
end

function pop_wnd_item:set_reddot_count(number)
    local number_str = "";
    if number > 99 then
        number_str = "99+"
    else
        number_str = tostring(number);
    end

    if number < 10 then
        self:set_data("Button_New_Message", "Visible", "true");
        self:set_data("Button_New_Message_Big", "Visible", "false");
        self:set_data("Button_New_Message", "Caption", number_str);
    else
        self:set_data("Button_New_Message", "Visible", "false");
        self:set_data("Button_New_Message_Big", "Visible", "true");
        self:set_data("Button_New_Message_Big", "Caption", number_str);
    end
end

function pop_wnd_item:get_talk_type_and_id()
    return self.msg_.type, self.msg_.contact_id;
end